﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Collections.Generic;//请先添加引用
namespace EnterpriseFund.DAL
{
    /// <summary>
    /// 数据访问类PrvFunctList。
    /// </summary>
    public class PrvFunctList
    {
        public PrvFunctList()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string FFunctNo, string FPreFunctNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_PrvFunctList");
            strSql.Append(" where FFunctNo=@FFunctNo and FPreFunctNo=@FPreFunctNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FFunctNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FPreFunctNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = FFunctNo;
            parameters[1].Value = FPreFunctNo;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Model.PrvFunctList model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_PrvFunctList(");
            strSql.Append("FFunctNo,FPreFunctNo,FfunctName,FfunctURL,FfunctType,Fnote,FFunctAlias,FPictName,FPictSelected,Fsort,fvisible,FfunctFullname)");
            strSql.Append(" values (");
            strSql.Append("@FFunctNo,@FPreFunctNo,@FfunctName,@FfunctURL,@FfunctType,@Fnote,@FFunctAlias,@FPictName,@FPictSelected,@Fsort,@fvisible,@FfunctFullname)");
            SqlParameter[] parameters = {
					new SqlParameter("@FFunctNo", SqlDbType.NVarChar,80),
					new SqlParameter("@FPreFunctNo", SqlDbType.NVarChar,80),
					new SqlParameter("@FfunctName", SqlDbType.NVarChar,40),
					new SqlParameter("@FfunctURL", SqlDbType.NVarChar,255),
					new SqlParameter("@FfunctType", SqlDbType.NChar,1),
					new SqlParameter("@Fnote", SqlDbType.NVarChar,200),
					new SqlParameter("@FFunctAlias", SqlDbType.NVarChar,20),
					new SqlParameter("@FPictName", SqlDbType.NVarChar,255),
					new SqlParameter("@FPictSelected", SqlDbType.NVarChar,255),
					new SqlParameter("@Fsort", SqlDbType.Int,4),
					new SqlParameter("@fvisible", SqlDbType.NChar,1),
					new SqlParameter("@FfunctFullname", SqlDbType.NVarChar,255)};
            parameters[0].Value = model.FFunctNo;
            parameters[1].Value = model.FPreFunctNo;
            parameters[2].Value = model.FfunctName;
            parameters[3].Value = model.FfunctURL;
            parameters[4].Value = model.FfunctType;
            parameters[5].Value = model.Fnote;
            parameters[6].Value = model.FFunctAlias;
            parameters[7].Value = model.FPictName;
            parameters[8].Value = model.FPictSelected;
            parameters[9].Value = model.Fsort;
            parameters[10].Value = model.fvisible;
            parameters[11].Value = model.FfunctFullname;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Model.PrvFunctList model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_PrvFunctList set ");
            strSql.Append("FfunctName=@FfunctName,");
            strSql.Append("FfunctURL=@FfunctURL,");
            strSql.Append("FfunctType=@FfunctType,");
            strSql.Append("Fnote=@Fnote,");
            strSql.Append("FFunctAlias=@FFunctAlias,");
            strSql.Append("FPictName=@FPictName,");
            strSql.Append("FPictSelected=@FPictSelected,");
            strSql.Append("Fsort=@Fsort,");
            strSql.Append("fvisible=@fvisible,");
            strSql.Append("FfunctFullname=@FfunctFullname");
            strSql.Append(" where FFunctNo=@FFunctNo and FPreFunctNo=@FPreFunctNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FFunctNo", SqlDbType.NVarChar,80),
					new SqlParameter("@FPreFunctNo", SqlDbType.NVarChar,80),
					new SqlParameter("@FfunctName", SqlDbType.NVarChar,40),
					new SqlParameter("@FfunctURL", SqlDbType.NVarChar,255),
					new SqlParameter("@FfunctType", SqlDbType.NChar,1),
					new SqlParameter("@Fnote", SqlDbType.NVarChar,200),
					new SqlParameter("@FFunctAlias", SqlDbType.NVarChar,20),
					new SqlParameter("@FPictName", SqlDbType.NVarChar,255),
					new SqlParameter("@FPictSelected", SqlDbType.NVarChar,255),
					new SqlParameter("@Fsort", SqlDbType.Int,4),
					new SqlParameter("@fvisible", SqlDbType.NChar,1),
					new SqlParameter("@FfunctFullname", SqlDbType.NVarChar,255)};
            parameters[0].Value = model.FFunctNo;
            parameters[1].Value = model.FPreFunctNo;
            parameters[2].Value = model.FfunctName;
            parameters[3].Value = model.FfunctURL;
            parameters[4].Value = model.FfunctType;
            parameters[5].Value = model.Fnote;
            parameters[6].Value = model.FFunctAlias;
            parameters[7].Value = model.FPictName;
            parameters[8].Value = model.FPictSelected;
            parameters[9].Value = model.Fsort;
            parameters[10].Value = model.fvisible;
            parameters[11].Value = model.FfunctFullname;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string FFunctNo, string FPreFunctNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_PrvFunctList ");
            strSql.Append(" where FFunctNo=@FFunctNo and FPreFunctNo=@FPreFunctNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FFunctNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FPreFunctNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = FFunctNo;
            parameters[1].Value = FPreFunctNo;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.PrvFunctList GetModel(string FFunctNo, string FPreFunctNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FFunctNo,FPreFunctNo,FfunctName,FfunctURL,FfunctType,Fnote,FFunctAlias,FPictName,FPictSelected,Fsort,fvisible,FfunctFullname from t_PrvFunctList ");
            strSql.Append(" where FFunctNo=@FFunctNo and FPreFunctNo=@FPreFunctNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FFunctNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FPreFunctNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = FFunctNo;
            parameters[1].Value = FPreFunctNo;

           Model.PrvFunctList model = new Model.PrvFunctList();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.FFunctNo = ds.Tables[0].Rows[0]["FFunctNo"].ToString();
                model.FPreFunctNo = ds.Tables[0].Rows[0]["FPreFunctNo"].ToString();
                model.FfunctName = ds.Tables[0].Rows[0]["FfunctName"].ToString();
                model.FfunctURL = ds.Tables[0].Rows[0]["FfunctURL"].ToString();
                model.FfunctType = ds.Tables[0].Rows[0]["FfunctType"].ToString();
                model.Fnote = ds.Tables[0].Rows[0]["Fnote"].ToString();
                model.FFunctAlias = ds.Tables[0].Rows[0]["FFunctAlias"].ToString();
                model.FPictName = ds.Tables[0].Rows[0]["FPictName"].ToString();
                model.FPictSelected = ds.Tables[0].Rows[0]["FPictSelected"].ToString();
                if (ds.Tables[0].Rows[0]["Fsort"].ToString() != "")
                {
                    model.Fsort = int.Parse(ds.Tables[0].Rows[0]["Fsort"].ToString());
                }
                model.fvisible = ds.Tables[0].Rows[0]["fvisible"].ToString();
                model.FfunctFullname = ds.Tables[0].Rows[0]["FfunctFullname"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FFunctNo,FPreFunctNo,FfunctName,FfunctURL,FfunctType,Fnote,FFunctAlias,FPictName,FPictSelected,Fsort,fvisible,FfunctFullname ");
            strSql.Append(" FROM t_PrvFunctList ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by Fsort");
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" FFunctNo,FPreFunctNo,FfunctName,FfunctURL,FfunctType,Fnote,FFunctAlias,FPictName,FPictSelected,Fsort,fvisible,FfunctFullname ");
            strSql.Append(" FROM t_PrvFunctList ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "t_PrvFunctList";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法


        #region 自己增加的方法

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList1(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FfunctName,FfunctType,Fnote,FFunctAlias,FPictName,FPictSelected,Fsort,fvisible,FPreFunctNo,FfunctURL,FFunctNo,FfunctFullname ");
            strSql.Append(" FROM t_PrvFunctList ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 得到一个对象实体,根据菜单编号
        /// </summary>
        public Model.PrvFunctList GetModelByFunctNo(string FFunctNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FfunctName,FfunctType,Fnote,FFunctAlias,FPictName,FPictSelected,Fsort,fvisible,FPreFunctNo,FfunctURL,FFunctNo,FfunctFullname from t_PrvFunctList ");
            strSql.Append(" where  FFunctNo=@FFunctNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FFunctNo", SqlDbType.VarChar,50)};
            parameters[0].Value = FFunctNo;

            Model.PrvFunctList model = new Model.PrvFunctList();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.FfunctName = ds.Tables[0].Rows[0]["FfunctName"].ToString();
                model.FfunctType = ds.Tables[0].Rows[0]["FfunctType"].ToString();
                model.Fnote = ds.Tables[0].Rows[0]["Fnote"].ToString();
                model.FFunctAlias = ds.Tables[0].Rows[0]["FFunctAlias"].ToString();
                model.FPictName = ds.Tables[0].Rows[0]["FPictName"].ToString();
                model.FPictSelected = ds.Tables[0].Rows[0]["FPictSelected"].ToString();
                if (ds.Tables[0].Rows[0]["Fsort"].ToString() != "")
                {
                    model.Fsort = int.Parse(ds.Tables[0].Rows[0]["Fsort"].ToString());
                }
                model.fvisible = ds.Tables[0].Rows[0]["fvisible"].ToString();
                model.FPreFunctNo = ds.Tables[0].Rows[0]["FPreFunctNo"].ToString();
                model.FfunctURL = ds.Tables[0].Rows[0]["FfunctURL"].ToString();
                model.FFunctNo = ds.Tables[0].Rows[0]["FFunctNo"].ToString();
                model.FfunctFullname = ds.Tables[0].Rows[0]["FfunctFullname"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }


        public DataSet getFunctList(List<string> str, string strEntityNo)
        {
            DataSet ds = new DataSet();


            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from t_PrvFunctList as funct inner join t_PrvGroupPriv as priv on(funct.FFunctNo=priv.FFunctNo)");
            strSql.Append(" where priv.FGroupNo in(");
            int t = 0;

            if (str.Count == 0)
            {
                strSql.Append("'");
                strSql.Append("'");
            }
            else
            {
                foreach (string s in str)
                {
                    t++;
                    if (t == str.Count)
                    {
                        strSql.Append("'");
                        strSql.Append(s);
                        strSql.Append("'");
                    }
                    else
                    {
                        strSql.Append("'");
                        strSql.Append(s);
                        strSql.Append("',");
                    }
                }
            }
            strSql.Append(") and priv.FEntityNo=@FEntityNo and funct.fvisible='Y' order by funct.Fsort");

            SqlParameter[] parareter ={
                                      new SqlParameter("@FEntityNo",SqlDbType.VarChar,50)                                    
                                      };

            parareter[0].Value = strEntityNo;

            ds = DbHelperSQL.Query(strSql.ToString(), parareter);
            return ds;

        }


        /// <summary>
        /// 获取菜单编号
        /// </summary>
        /// <param name="str"></param>
        /// <param name="strEntityNo"></param>
        /// <returns></returns>
        public List<string> getFunctNo(string str, string strEntityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select funct.FFunctNo from t_PrvFunctList as funct inner join t_PrvGroupPriv as priv on(funct.FFunctNo=priv.FFunctNo or funct.FFunctSynonymsNo=priv.FFunctNo)");
            strSql.Append(" where priv.FGroupNo in(");
            strSql.Append(str);
            strSql.Append(") and priv.FEntityNo=@FEntityNo and funct.fvisible='Y' order by funct.Fsort");

            SqlParameter[] parameters ={
                                      new SqlParameter("@FEntityNo",SqlDbType.VarChar,50)                                    
                                      };

            parameters[0].Value = strEntityNo;

            return DbHelperSQL.getListString(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 更新一条数据，根据菜单项编号
        /// </summary>
        public void UpdateByFunctNo(Model.PrvFunctList model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_PrvFunctList set ");
            strSql.Append("FfunctType=@FfunctType,");
            strSql.Append("Fnote=@Fnote,");
            strSql.Append("FFunctAlias=@FFunctAlias,");
            strSql.Append("FPictName=@FPictName,");
            strSql.Append("FPictSelected=@FPictSelected,");
            strSql.Append("Fsort=@Fsort,");
            strSql.Append("fvisible=@fvisible,");
            strSql.Append("FPreFunctNo=@FPreFunctNo,");
            strSql.Append("FfunctURL=@FfunctURL,");
            strSql.Append("FfunctName=@FfunctName,");
            strSql.Append("FfunctFullname=@FfunctFullname");
            strSql.Append(" where FFunctNo=@FFunctNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FfunctName", SqlDbType.VarChar,40),
					new SqlParameter("@FfunctType", SqlDbType.Char,1),
					new SqlParameter("@Fnote", SqlDbType.VarChar,200),
					new SqlParameter("@FFunctAlias", SqlDbType.VarChar,20),
					new SqlParameter("@FPictName", SqlDbType.VarChar,255),
					new SqlParameter("@FPictSelected", SqlDbType.VarChar,255),
					new SqlParameter("@Fsort", SqlDbType.Int,4),
					new SqlParameter("@fvisible", SqlDbType.Char,1),
					new SqlParameter("@FPreFunctNo", SqlDbType.VarChar,255),
					new SqlParameter("@FfunctURL", SqlDbType.VarChar,255),
					new SqlParameter("@FFunctNo", SqlDbType.VarChar,80),
					new SqlParameter("@FfunctFullname", SqlDbType.VarChar,255)};
            parameters[0].Value = model.FfunctName;
            parameters[1].Value = model.FfunctType;
            parameters[2].Value = model.Fnote;
            parameters[3].Value = model.FFunctAlias;
            parameters[4].Value = model.FPictName;
            parameters[5].Value = model.FPictSelected;
            parameters[6].Value = model.Fsort;
            parameters[7].Value = model.fvisible;
            parameters[8].Value = model.FPreFunctNo;
            parameters[9].Value = model.FfunctURL;
            parameters[10].Value = model.FFunctNo;
            parameters[11].Value = model.FfunctFullname;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 删除一条数据,根据菜单编号
        /// </summary>
        public void DeleteByFunctNo(string FFunctNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_PrvFunctList ");
            strSql.Append(" where FFunctNo=@FFunctNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FFunctNo", SqlDbType.VarChar,50)};
            parameters[0].Value = FFunctNo;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }






        #endregion


    }
}

